const Type = (any) =>
    Object.prototype.toString.call(any).replace(/\[object (.*?)\]/g, "$1");
function isEmpty(value) {
    if (!value) {
        return true;
    }
    if (Type(value) === "Array") {
        return !value.length;
    }
    for (let _ in value) {
        return false;
    }
    return true;
}
function handleArray(inputArray, [target], errors = {}) {
    const targetType = Type(target);
    let temp;
    switch (targetType) {
        case "String":
            // ["Number"]
            inputArray.forEach((item, index) => {
                if (Type(item) !== target) {
                    errors[index] = [target, item];
                }
            });
            break;
        case "Object":
            // [{ name: "String" }]
            inputArray.forEach((item, index) => {
                temp = checkTypeLoop(item, target);
                if (!isEmpty(temp)) errors[index] = temp;
            });
            break;
    }
    return errors;
}
function checkTypeLoop(input, template, errors = {}) {
    Object.entries(template).forEach(([key, correctType]) => {
        if (!input || !input.hasOwnProperty(key))
            errors[key] = [correctType, undefined];
        let temp;
        switch (Type(correctType)) {
            case "Object":
                temp = checkTypeLoop(input[key], correctType);
                if (!isEmpty(temp)) errors[key] = temp;
                break;
            case "Array":
                temp = handleArray(input[key], correctType);
                if (!isEmpty(temp)) errors[key] = temp;
                break;
            default:
                const type = Type(input[key]);
                if (type !== correctType)
                    errors[key] = [correctType, input[key]];
        }
    });
    return errors;
}
module.exports = function isTypeDanger(input, template) {
    const res = checkTypeLoop(input, template);
    if (isEmpty(res)) {
        return false;
    } else {
        return JSON.stringify(res);
    }
};
